Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Nested function</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Nested_function"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Nested_function rootpage-Nested_function skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Nested function</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>nested function</b> (or <b>nested procedure</b> or <b>subroutine</b>) is a <a href="Identifier" title="Identifier">named</a> <a href="Subroutine" class="mw-redirect" title="Subroutine">function</a> that is defined within another, enclosing, block and is <a href="Lexically_scoped" class="mw-redirect" title="Lexically scoped">lexically scoped</a> within the enclosing block – meaning it is only callable by name within the body of the enclosing block and can use <a href="Identifiers" class="mw-redirect" title="Identifiers">identifiers</a> declared in outer <a href="Block_(programming)" title="Block (programming)">blocks</a>, including outer functions. The enclosing block is typically, but not always, another function.
</p><p><a href="Programming_language" title="Programming language">Programming language</a> support for nested functions varies. With respect to <a href="Structured_programming" title="Structured programming">structured programming</a> languages, it is supported in some outdated languages such as <a href="ALGOL" title="ALGOL">ALGOL</a>, <a href="Simula_67" class="mw-redirect" title="Simula 67">Simula 67</a> and <a href="Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a> and in the commonly used <a href="JavaScript" title="JavaScript">JavaScript</a>. It is commonly supported in <a href="Dynamic_language" class="mw-redirect" title="Dynamic language">dynamic</a> and <a href="Functional_language" class="mw-redirect" title="Functional language">functional</a> languages.
However, it is not supported in some commonly used languages including standard <a href="C_language" class="mw-redirect" title="C language">C</a> and <a href="C%2B%2B" title="C++">C++</a>.
</p><p>Other programming technologies provide similar benefit. For example, a <a href="Lambda_function_(computer_programming)" class="mw-redirect" title="Lambda function (computer programming)">lambda function</a> also allows for a function to be defined inside of a function (as well as elsewhere) and allows for similar data hiding and encapsulation. Notably, a lambda function has no name (is anonymous) and therefore cannot be called by name and has no visibility aspect.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Attributes">Attributes</h2></div>
<p>The <a href="Scope_(computer_science)" title="Scope (computer science)">scope</a> of a nested function is the block that contains it – be it a function block or block within a function body. It is not visible (cannot be called by name) outside its containing block.
</p><p>A nested function can use <a href="Identifiers" class="mw-redirect" title="Identifiers">identifiers</a> (i.e. the name of functions, variables, types, classes) declared in any enclosing block, except when they are masked by inner declarations with the same names.
</p><p>A nested function can be declared within a nested function, recursively, to form a deeply nested structure.
A deeply nested function can access identifiers declared in all of its enclosing blocks, including enclosing functions.
</p><p>Nested functions may in certain situations lead to the creation of a <a href="Closure_(computer_programming)" title="Closure (computer programming)">closure</a>. If it is possible for the nested function to <a href="Escape_analysis" title="Escape analysis">escape</a> the enclosing function, for example if functions are <a href="First_class_object" class="mw-redirect" title="First class object">first class objects</a> and a nested function is passed to another function or returned from the enclosing function, then a closure is created and calls to this function can access the environment of the original function. The frame of the immediately enclosing function must continue to be alive until the last referencing closure dies and <a href="Non-local_variable" title="Non-local variable">non-local</a> <a href="Automatic_variable" title="Automatic variable">automatic variables</a> referenced in closures can therefore not be <a href="Stack_allocation" class="mw-redirect" title="Stack allocation">stack allocated</a> in languages that allow the closure to persist beyond the lifetime of the enclosing block. This is known as the <a href="Funarg_problem" title="Funarg problem">funarg problem</a> and is a key reason why nested functions was not implemented in some simpler languages as it significantly complicates code generation and analysis, especially when functions are nested to various levels, sharing different parts of their environment.
</p>
<div class="mw-heading mw-heading2"><h2 id="Value">Value</h2></div>
<p>The nested function technology allows a <a href="Programmer" title="Programmer">programmer</a> to write <a href="Source_code" title="Source code">source code</a> that includes beneficial attributes such as <a href="Information_hiding" title="Information hiding">information hiding</a>, <a href="Encapsulation_(computer_programming)" title="Encapsulation (computer programming)">encapsulation</a> and <a href="Decomposition_(computer_science)" title="Decomposition (computer science)">decomposition</a>. The programmer can divide a task into subtasks which are only meaningful within the context of the task such that the subtask functions are hidden from callers that are not designed to use them.
</p><p>Block scoping allows functions to share the state of enclosing blocks (including enclosing functions) without passing <a href="Parameter_(computer_programming)" title="Parameter (computer programming)">parameters</a> or using <a href="Global_variable" title="Global variable">global variables</a>.<sup id="cite_ref-FOOTNOTEBright2004_1-0" class="reference"><a href="#cite_note-FOOTNOTEBright2004-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Uses">Uses</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Helper">Helper</h3></div>
<p>A nested function typically acts as a <a href="Wrapper_function" title="Wrapper function">helper function</a> or a <a href="Recursion_(computer_science)" title="Recursion (computer science)">recursive function</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Control_flow">Control flow</h3></div>
<p>Nested functions can be used for unstructured <a href="Control_flow" title="Control flow">control flow</a>, by using the return statement for general unstructured control flow. This can be used for finer-grained control than is possible with other built-in features of the language – for example, it can allow early termination of a for loop if <code>break</code> is not available, or early termination of a nested <a href="For_loop" title="For loop">for loop</a> if a multi-level <code>break</code> or exceptions are not available.
</p>
<div class="mw-heading mw-heading3"><h3 id="Higher-order_functions">Higher-order functions</h3></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Higher-order_function" title="Higher-order function">Higher-order function</a></div>
<p>In some languages, it is possible to create a nested function that accesses a set of parameters from the outer function, that is a <a href="Closure_(computer_programming)" title="Closure (computer programming)">closure</a>, and have that function be the outer function's return value. Thus it is possible to return a function that is set to fulfill a certain task with little or no further parameters given to it, which can increase performance quite significantly.<sup id="cite_ref-Higher-Order_Functions_and_Lambdas_–_Kotlin_Programming_Language_2-0" class="reference"><a href="#cite_note-Higher-Order_Functions_and_Lambdas_–_Kotlin_Programming_Language-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Simple_example">Simple example</h3></div>
<p>A simple example in Pascal:
</p>
<div class="mw-highlight mw-highlight-lang-pascal mw-content-ltr" dir="ltr"><pre><span class="k">function</span><span class="w"> </span><span class="nf">E</span><span class="p">(</span><span class="n">x</span><span class="o">:</span><span class="w"> </span><span class="kt">real</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="kt">real</span><span class="o">;</span>
<span class="w"> </span><span class="k">function</span><span class="w"> </span><span class="nf">F</span><span class="p">(</span><span class="n">y</span><span class="o">:</span><span class="w"> </span><span class="kt">real</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="kt">real</span><span class="o">;</span>
<span class="w"> </span><span class="k">begin</span>
<span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">y</span>
<span class="w"> </span><span class="k">end</span><span class="o">;</span>
<span class="k">begin</span>
<span class="w"> </span><span class="n">E</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">F</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">F</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="k">end</span><span class="o">;</span>
</pre></div>
<p>The function <code>F</code> is nested within <code>E</code>. Note that <code>E</code>'s parameter <code>x</code> is also visible in <code>F</code> (as <code>F</code> is a part of <code>E</code>) while both <code>x</code> and <code>y</code> are invisible outside <code>E</code> and <code>F</code> respectively.
</p><p>Similarly, in <a href="Standard_ML" title="Standard ML">Standard ML</a>:
</p>
<div class="mw-highlight mw-highlight-lang-ocaml mw-content-ltr" dir="ltr"><pre><span class="k">fun</span> <span class="n">e</span> <span class="o">(</span><span class="n">x</span> <span class="o">:</span> <span class="n">real</span><span class="o">)</span> <span class="o">=</span>
<span class="k">let</span>
<span class="k">fun</span> <span class="n">f</span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span>
<span class="k">in</span>
<span class="n">f</span> <span class="mi">3</span> <span class="o">+</span> <span class="n">f</span> <span class="mi">4</span>
<span class="k">end</span><span class="o">;</span>
</pre></div>
<p>In <a href="Haskell_(programming_language)" class="mw-redirect" title="Haskell (programming language)">Haskell</a>:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="nf">e</span><span class="w"> </span><span class="ow">::</span><span class="w"> </span><span class="kt">Float</span><span class="w"> </span><span class="ow">-&gt;</span><span class="w"> </span><span class="kt">Float</span>
<span class="nf">e</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="kr">where</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">y</span>
</pre></div>
<p>In <a href="PL/I" title="PL/I">PL/I</a>:
</p>
<style data-mw-deduplicate="TemplateStyles:r1195917819">
/* start https://en.wikipedia.org/ */


.mw-parser-output .pre-borderless{border:none}


/* end https://en.wikipedia.org/ */
</style><pre class="pre">e: procedure(x) returns(float);
declare x float;
f: procedure(y) returns(float);
declare y float;
return x + y
end;
return f(3.0) + f(4.0);
end;</pre>
<p>In <a href="Python_(programming_language)" title="Python (programming language)">Python</a>:
</p>
<div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="k">def</span> <span class="nf">e</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">y</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="k">return</span> <span class="n">f</span><span class="p">(</span><span class="mf">3.0</span><span class="p">)</span> <span class="o">+</span> <span class="n">f</span><span class="p">(</span><span class="mf">4.0</span><span class="p">)</span>
</pre></div>
<p>In <a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GNU C</a><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> – which extends standard C with nested functions:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">float</span><span class="w"> </span><span class="nf">E</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="n">x</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">F</span><span class="p">(</span><span class="kt">float</span><span class="w"> </span><span class="n">y</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">y</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">F</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">F</span><span class="p">(</span><span class="mi">4</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Quicksort">Quicksort</h3></div>
<p>The following is an implementation of <a href="Quicksort" title="Quicksort">quicksort</a>:<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">sort</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">items</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">quickSort</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">first</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">last</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">q</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">items</span><span class="p">[</span><span class="n">p</span><span class="p">];</span>
<span class="w"> </span><span class="n">items</span><span class="p">[</span><span class="n">p</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">items</span><span class="p">[</span><span class="n">q</span><span class="p">];</span>
<span class="w"> </span><span class="n">items</span><span class="p">[</span><span class="n">q</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">partition</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">pivot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">items</span><span class="p">[</span><span class="n">first</span><span class="p">],</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">first</span><span class="p">;</span>
<span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">index</span><span class="p">,</span><span class="w"> </span><span class="n">last</span><span class="p">);</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">first</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">last</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">items</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">pivot</span><span class="p">)</span>
<span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">index</span><span class="o">++</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">index</span><span class="p">,</span><span class="w"> </span><span class="n">last</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">index</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>

<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">first</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">last</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">pivotIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">partition</span><span class="p">();</span>
<span class="w"> </span><span class="n">quickSort</span><span class="p">(</span><span class="n">first</span><span class="p">,</span><span class="w"> </span><span class="n">pivotIndex</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="n">quickSort</span><span class="p">(</span><span class="n">pivotIndex</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">last</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">quickSort</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>The following is an implementation of the <a href="Quicksort#Hoare_partition_scheme" title="Quicksort">Hoare partition based quicksort</a> using <a href="C%2B%2B11#Lambda_functions_and_expressions" title="C++11">C++11</a> <a href="Anonymous_function#C.2B.2B_.28since_C.2B.2B11.29" title="Anonymous function">lambda expression syntax</a> which is an alternative technology that also allows hiding a function inside a function:
</p>
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="k">template</span><span class="o">&lt;</span><span class="k">typename</span><span class="w"> </span><span class="nc">RandomAccessIterator</span><span class="o">&gt;</span>
<span class="k">auto</span><span class="w"> </span><span class="n">Sort</span><span class="p">(</span><span class="n">RandomAccessIterator</span><span class="w"> </span><span class="n">Begin</span><span class="p">,</span><span class="w"> </span><span class="n">RandomAccessIterator</span><span class="w"> </span><span class="n">End</span><span class="p">)</span><span class="o">-&gt;</span><span class="kt">void</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">Partition</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="p">]()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">//Hoare partition scheme</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Pivot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">Begin</span><span class="p">;</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">ForwardCursor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Begin</span><span class="p">;</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">BackwardCursor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">End</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">PartitionPositionFound</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">false</span><span class="p">;</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">LocatePartitionPosition</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="p">]()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">ForwardCursor</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">Pivot</span><span class="p">)</span>
<span class="w"> </span><span class="o">++</span><span class="n">ForwardCursor</span><span class="p">;</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">Pivot</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="o">*</span><span class="n">BackwardCursor</span><span class="p">)</span>
<span class="w"> </span><span class="o">--</span><span class="n">BackwardCursor</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">ForwardCursor</span><span class="w"> </span><span class="o">&gt;=</span><span class="w"> </span><span class="n">BackwardCursor</span><span class="p">)</span>
<span class="w"> </span><span class="n">PartitionPositionFound</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">true</span><span class="p">;</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="n">Swap</span><span class="p">(</span><span class="o">*</span><span class="n">ForwardCursor</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">BackwardCursor</span><span class="p">);</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="c1">//Trivial helper function</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">MoveOnAndTryAgain</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="p">]()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">++</span><span class="n">ForwardCursor</span><span class="p">;</span>
<span class="w"> </span><span class="o">--</span><span class="n">BackwardCursor</span><span class="p">;</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="c1">//Brief outline of the actual partition process</span>
<span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nb">true</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">LocatePartitionPosition</span><span class="p">();</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PartitionPositionFound</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">BackwardCursor</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="k">else</span>
<span class="w"> </span><span class="n">MoveOnAndTryAgain</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">};</span>
<span class="w"> </span><span class="c1">//Brief outline of the quicksort algorithm</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">Begin</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">End</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">PartitionPosition</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Partition</span><span class="p">();</span>
<span class="w"> </span><span class="n">Sort</span><span class="p">(</span><span class="n">Begin</span><span class="p">,</span><span class="w"> </span><span class="n">PartitionPosition</span><span class="p">);</span>
<span class="w"> </span><span class="n">Sort</span><span class="p">(</span><span class="n">PartitionPosition</span><span class="p">,</span><span class="w"> </span><span class="n">End</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Languages">Languages</h2></div>
<p>Notable languages supporting nested functions include:
</p>
<ul><li><a href="ALGOL" title="ALGOL">ALGOL</a>-based languages such as <a href="ALGOL_68" title="ALGOL 68">ALGOL 68</a>, <a href="Simula" title="Simula">Simula</a>, <a href="Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a>, <a href="Modula-2" title="Modula-2">Modula-2</a>, <a href="Modula-3" title="Modula-3">Modula-3</a>, <a href="Oberon_(programming_language)" title="Oberon (programming language)">Oberon</a>, <a href="PL/I" title="PL/I">PL/I</a>, <a href="Seed7" title="Seed7">Seed7</a> and <a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a></li>
<li>Modern versions of <a href="Lisp_(programming_language)" title="Lisp (programming language)">Lisp</a> (with lexical scope) such as <a href="Scheme_(programming_language)" title="Scheme (programming language)">Scheme</a>, and <a href="Common_Lisp" title="Common Lisp">Common Lisp</a></li>
<li><a href="ECMAScript" title="ECMAScript">ECMAScript</a> (<a href="JavaScript" title="JavaScript">JavaScript</a> and <a href="ActionScript" title="ActionScript">ActionScript</a>)</li>
<li><a href="Dart_(programming_language)" title="Dart (programming language)">Dart</a><sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Kotlin_(programming_language)" title="Kotlin (programming language)">Kotlin</a> (local functions<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>)</li>
<li><a href="Rust_(programming_language)" title="Rust (programming language)">Rust</a></li>
<li><a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a> (nested functions<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup>)</li>
<li>Various degrees of support in scripting languages such as <a href="Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>, <a href="Python_(programming_language)" title="Python (programming language)">Python</a>, <a href="Lua_(programming_language)" class="mw-redirect" title="Lua (programming language)">Lua</a>, <a href="PHP" title="PHP">PHP</a> and <a href="Perl" title="Perl">Perl</a></li>
<li><a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GCC</a> supports nested functions in C, as a language extension.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup></li>
<li><a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>, starting with C# 7.0</li>
<li>The <a href="D_(programming_language)" title="D (programming language)">D</a> language, a C-related language with nested functions.</li>
<li><a href="Fortran" title="Fortran">Fortran</a>, starting with <a href="Fortran#Fortran_90" title="Fortran">Fortran-90</a>, supports <i>a single level</i> of nested (<i>CONTAINed</i>) subroutines and functions.</li>
<li><a href="MATLAB" title="MATLAB">MATLAB</a> (full support)</li>
<li><a href="Wolfram_Language" title="Wolfram Language">Wolfram Language</a></li>
<li><a href="Go_(programming_language)" title="Go (programming language)"> Golang</a> (Function closures<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup>)</li></ul>
<div class="mw-heading mw-heading3"><h3 id="Functional_languages">Functional languages</h3></div>
<p>In most <a href="Functional_programming" title="Functional programming">functional programming</a> languages, such as Scheme, nested functions are a <a href="Programming_idiom" title="Programming idiom">common way</a> of implementing <a href="Algorithm" title="Algorithm">algorithms</a> with loops in them. A simple (<a href="Tail_recursion" class="mw-redirect" title="Tail recursion">tail</a>) <a href="Recursion" title="Recursion">recursive</a> inner function is created, which behaves as the algorithm's main loop, while the outer function performs startup actions that only need to be done once. In more complex cases, a number of mutually recursive functions may be created as inner functions.
</p>
<div class="mw-heading mw-heading2"><h2 id="Alternatives">Alternatives</h2></div>
<p>Various alternative techniques can be used to achieve similar programming results as via nested functions.
</p>
<div class="mw-heading mw-heading3"><h3 id="Modularity">Modularity</h3></div>
<p>A common alternative is to leverage a language's modularity technology. Some functions are exposed for use outside of the module and some are only visible within the module.
</p><p>In C, this can be implemented by declaring functions and variables as <i>static</i> to hide them from code outside the file.<sup id="cite_ref-cfaq_10-0" class="reference"><a href="#cite_note-cfaq-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> This allows for data hiding, encapsulation and decomposition, but at a different level of <a href="Granularity" title="Granularity">granularity</a> than with nested functions. This modularity does not support more than one level of nesting.
</p><p>In <a href="Object-oriented_languages" class="mw-redirect" title="Object-oriented languages">object-oriented languages</a>, a class typically provides a scope in which functions and state can be hidden from consumers of the class but accessible within the class. Some languages allow classes to be nested.
</p>
<div class="mw-heading mw-heading3"><h3 id="Parameters">Parameters</h3></div>
<p>To implement data hiding, functions can pass around shared data as parameters, but this increases the complexity of function calls.<sup id="cite_ref-FOOTNOTEBright2004_1-1" class="reference"><a href="#cite_note-FOOTNOTEBright2004-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p><p>In C, this is generally implemented by passing a pointer to a structure containing the shared data.<sup id="cite_ref-cfaq_10-1" class="reference"><a href="#cite_note-cfaq-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Lambda">Lambda</h3></div>
<p>In <a href="PHP" title="PHP">PHP</a> and other languages, the <a href="Anonymous_function" title="Anonymous function">lambda</a> is an alternative. A function is defined in a code statement rather than declared with the usual function syntax. It has no name but is callable via a <a href="Function_reference" class="mw-redirect" title="Function reference">function reference</a>. Such functions can be defined inside of a function as well as in other scopes. To use local variables in the anonymous function, use <a href="Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">closure</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Alternatives_by_language">Alternatives by language</h3></div>
<p>The following languages provide features that are similar to nested functions:
</p>
<ul><li><a href="C%2B%2B" title="C++">C++</a> – classes allow for similar data hiding and encapsulation; defining a class within a class provides similar structure (see <a href="Function_object#In_C_and_C.2B.2B" title="Function object">Function object in C++</a>)</li></ul>
<ul><li><a href="C%2B%2B11" title="C++11">C++11</a> and later – via lambda expressions (see quicksort example above)<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup></li></ul>
<ul><li><a href="Eiffel_(programming_language)" title="Eiffel (programming language)">Eiffel</a> – explicitly disallows nesting of routines to keep the language simple; does allow the convention of using a special variable, <b>Result</b>, to denote the result of a (value-returning) function</li></ul>
<ul><li><a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a> and <a href="Visual_Basic_.NET" class="mw-redirect" title="Visual Basic .NET">Visual Basic</a> – via lambda expressions</li></ul>
<ul><li><a href="Java_(programming_language)" title="Java (programming language)">Java</a> – since Java 8, via <a href="Anonymous_function#Java" title="Anonymous function">lambda expressions</a><sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>, and in older versions, via an <a href="Anonymous_class" class="mw-redirect" title="Anonymous class">anonymous class</a> containing a single method</li></ul>
<div class="mw-heading mw-heading2"><h2 id="Implementation">Implementation</h2></div>
<div role="note" class="hatnote navigation-not-searchable">See also: <a href="Man_or_boy_test" title="Man or boy test">Man or boy test</a></div>
<p>Implementation of nested functions can be more involved than it may appear, as a reference to a nested function that references non-local variables creates a <a href="Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">closure</a>. For this reason nested functions are not supported in some languages such as C, C++ or Java as this makes compilers more difficult to implement.<sup id="cite_ref-cfaq_10-2" class="reference"><a href="#cite_note-cfaq-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup> However, some compilers do support them, as a compiler specific extension. A well known example of this is the <a href="GNU_C" class="mw-redirect" title="GNU C">GNU C</a> implementation of C which shares code with compilers for languages such as Pascal, Ada and Modula.
</p>
<div class="mw-heading mw-heading3"><h3 id="Access_of_non-local_objects">Access of non-local objects</h3></div>
<p>There are several ways to implement nested procedures in a lexically scoped language, but the classic way is as follows:
</p>
<dl><dd>Any <a href="Non-local_object" class="mw-redirect" title="Non-local object">non-local object</a>, X, is reached via access-links in the <a href="Call_stack" title="Call stack">activation frames</a> on the machine stack. The caller, C, assists the called procedure, P, by pushing a <i>direct</i> link to the <i>latest</i> activation of P's immediate lexical encapsulation, (P), prior to the call itself. P may then quickly find the right activation for a certain X by following a <i>fixed number</i> (P.depth – X.depth) of links (normally a small number).</dd></dl>
<dl><dd>The caller creates this direct link by (itself) following C.depth – P.depth + 1 older links, leading up to the latest activation of (P), and then <i>temporarily</i> bridging over these with a direct link to that activation; the link later disappears together with P, whereby the older links beneath it may come into use again.</dd></dl>
<dl><dd>Note that P is visible for, and may therefore be called by, C if (P) = C / (C) / ((C)) / etc.</dd></dl>
<p>This original method is faster than it may seem, but it is nevertheless often optimized in practical modern compilers (using <a href="Call_stack#Display" title="Call stack"><i>displays</i></a> or similar techniques).
</p><p>Another way to implement nested functions that is used by some compilers is to convert ("lift") nested functions into non-nested functions (where extra, hidden, parameters replace the access links) using a process known as <a href="Lambda_lifting" title="Lambda lifting">lambda lifting</a> during an intermediate stage in the compilation.
</p>
<div class="mw-heading mw-heading3"><h3 id="Functions_as_values">Functions as values</h3></div>
<p>In order for local functions with <a href="Lexically_scoped" class="mw-redirect" title="Lexically scoped">lexically scoped</a> <a href="Non-local_variable" title="Non-local variable">nonlocals</a> to be passed as results, the language runtime code must also implicitly pass the environment (data) that the function sees inside its encapsulating function, so that it is reachable also when the current activation of the enclosing function no longer exists.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> This means that the environment must be stored in another memory area than (the subsequently reclaimed parts of) a chronologically based execution stack, which, in turn, implies some sort of freely <a href="Dynamic_memory_allocation" class="mw-redirect" title="Dynamic memory allocation">dynamic memory allocation</a>. Many older Algol based languages (or dialects thereof) does therefore not allow local functions that access nonlocals to be passed as return values, or do they not allow functions as return values at all, although passing of such functions as arguments may still be possible.
</p>
<div class="mw-heading mw-heading3"><h3 id="No-execute_stacks">No-execute stacks</h3></div>
<p><a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GCC</a>'s implementation of nested functions causes a loss of <a href="NX_bit" title="NX bit">no-execute</a> <a href="Call_stack" title="Call stack">stacks</a> (NX stacks). This implementation calls nested functions through a <a href="Jump_instruction" class="mw-redirect" title="Jump instruction">jump instruction</a> placed on the machine stack at runtime. This requires the stack to be executable. No-execute stacks and nested functions are therefore mutually exclusive in GCC. If a nested function is used in the development of a program, then the NX stack is silently lost, unless GCC is called with the <code>‑Wtrampoline</code> option to alert of the condition. Software engineered using <a href="Software_Development_Security" class="mw-redirect" title="Software Development Security">Secure Development Lifecycle</a> often do not allow the use of nested functions in this particular compiler due to the loss of NX stacks.<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Call_stack" title="Call stack">Call stack</a></li>
<li><a href="Closure_(computer_science)" class="mw-redirect" title="Closure (computer science)">Closure (computer science)</a></li>
<li><a href="Function_composition_(computer_science)" title="Function composition (computer science)">Function composition (computer science)</a></li>
<li><a href="Inner_class" title="Inner class">Inner class</a></li>
<li><a href="Nesting_(computing)" title="Nesting (computing)">Nesting (computing)</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-FOOTNOTEBright2004-1"><span class="mw-cite-backlink">^ <a href="#cite_ref-FOOTNOTEBright2004_1-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-FOOTNOTEBright2004_1-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><a href="#CITEREFBright2004">Bright 2004</a>.</span>
</li>
<li id="cite_note-Higher-Order_Functions_and_Lambdas_–_Kotlin_Programming_Language-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-Higher-Order_Functions_and_Lambdas_–_Kotlin_Programming_Language_2-0">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://kotlinlang.org/docs/reference/inline-functions.html">Higher-Order Functions and Lambdas - Kotlin Programming Language</a></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFRothwell2011" class="citation book cs1">Rothwell, Trevis J. (2011). <i>The GNU C Reference Manual</i>. Free Software Foundation, Inc. p.&nbsp;63.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text">
<a rel="nofollow" class="external text" href="https://archive.today/20130703055646/http://www.dreamincode.net/forums/topic/262883-nesting-functions-why/page__p__1530693&amp;%23entry1530693">Re: Nesting functions- Why?</a>, <a rel="nofollow" class="external text" href="https://web.archive.org/web/20100405113726/http://www.dreamincode.net/forums/user/52176-baavgai/">baavgai</a>, 14 January 2012</span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://dart.dev/guides/language/language-tour#lexical-scope">"A tour of the Dart language"</a>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://kotlinlang.org/docs/functions.html#local-functions">"Functions | Kotlin"</a>.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.scala-lang.org/tour/nested-functions.html">"Nested Methods"</a>.</cite></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html">"Nested Functions – Using the GNU Compiler Collection (GCC)"</a>. GNU Project<span class="reference-accessdate">. Retrieved <span class="nowrap">2007-01-06</span></span>.</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://go.dev/tour/moretypes/25">"A tour of Go"</a>.</cite></span>
</li>
<li id="cite_note-cfaq-10"><span class="mw-cite-backlink">^ <a href="#cite_ref-cfaq_10-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-cfaq_10-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-cfaq_10-2"><sup><i><b>c</b></i></sup></a></span> <span class="reference-text">"<a rel="nofollow" class="external text" href="http://c-faq.com/misc/nestfcns.html">Question 20.24: Why doesn't C have nested functions?</a>, comp.lang.c FAQ</span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.rosettacode.org/wiki/Nested_function#C.2B.2B">"Nested function - Rosetta Code"</a>.</cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.rosettacode.org/wiki/Nested_function#Java">"Nested function - Rosetta Code"</a>.</cite></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://stackoverflow.com/a/1348456/2025416">answer</a> by Dave Vandervies, Aug 28 '09 at 17:45, to "<a rel="nofollow" class="external text" href="https://stackoverflow.com/questions/1348095/why-are-nested-functions-not-supported-by-the-c-standard">Why are nested functions not supported by the C standard?</a>"</span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text">Such a combination of function code and its environment is sometimes called a <a href="Closure_(computer_programming)" title="Closure (computer programming)">closure</a>.</span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><cite id="CITEREFWalton" class="citation web cs1">Walton, Jeffrey. <a rel="nofollow" class="external text" href="http://www.owasp.org/index.php/C-Based_Toolchain_Hardening#GCC.2FBinutils">"C-Based Toolchain Hardening"</a>. The Open Web Application Security Project (OWASP)<span class="reference-accessdate">. Retrieved <span class="nowrap">28 February</span> 2017</span>.</cite></span>
</li>
</ol></div></div>
<style data-mw-deduplicate="TemplateStyles:r1239549316">
/* start https://en.wikipedia.org/ */


.mw-parser-output .refbegin{margin-bottom:0.5em}.mw-parser-output .refbegin-hanging-indents>ul{margin-left:0}.mw-parser-output .refbegin-hanging-indents>ul>li{margin-left:0;padding-left:3.2em;text-indent:-3.2em}.mw-parser-output .refbegin-hanging-indents ul,.mw-parser-output .refbegin-hanging-indents ul li{list-style:none}@media(max-width:720px){.mw-parser-output .refbegin-hanging-indents>ul>li{padding-left:1.6em;text-indent:-1.6em}}.mw-parser-output .refbegin-columns{margin-top:0.3em}.mw-parser-output .refbegin-columns ul{margin-top:0}.mw-parser-output .refbegin-columns li{page-break-inside:avoid;break-inside:avoid-column}@media screen{.mw-parser-output .refbegin{font-size:90%}}


/* end https://en.wikipedia.org/ */
</style><div class="refbegin" style="">
<ul><li><cite id="CITEREFBright2004" class="citation web cs1">Bright, Walter (1 May 2004). <a rel="nofollow" class="external text" href="http://www.drdobbs.com/nested-functions/184401792">"Nested Functions"</a>. <i><a href="Dr._Dobb's" class="mw-redirect" title="Dr. Dobb's">Dr. Dobb's</a></i>.</cite></li></ul>
</div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://c-faq.com/misc/nestfcns.html">comp.lang.c FAQ: Nested Functions</a></li>
<li><a rel="nofollow" class="external text" href="http://www.freepascal.org/docs-html/prog/progse23.html">"6.4 Nested procedure and functions"</a>. FreePascal documentation.</li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-07-17" href="https://en.wikipedia.org/wiki/?title=Nested_function&amp;oldid=1300993673">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>